<template>
	<div
	  class="diy_edit article_type_edits page_i_want_to_customize"
	  id="i_want_to_customize_edit"
	>
	  <el-form
		:model="form"
		ref="ruleFormRef"
		:rules="rules"
		label-width="auto"
		class="edit_article_type_form"
		size="large"
	  >
		<el-form-item label="分类名称" prop="name">
		  <el-input v-model="form.name" />
		</el-form-item>
		<div class="footer">
		  <el-button type="primary" @click="Onsubmit" size="large"
			>提交</el-button
		  >
		  <el-button @click="cancel()" size="large">取消</el-button>
		</div>
	  </el-form>
	</div>
  </template>
  
  <script setup>
  import { ElMessage } from "element-plus";
  import { ref, onMounted } from "vue";
  import { $post, $get } from "@/api/Api.js";
  import storageHelper from "@/libs/StorageHelper";
  import { useStore } from "vuex";
  import { useRouter } from "vue-router";
  import {
	$get_auth,
	func,
	paramsFun,
	$fullUrl,
	formatDate,
	$banseUrl,
	$check_action,
	$getUrlParams,
	filterSensitiveWords,
  } from "@/utils/utils.js";
  let router = useRouter();
  let store = useStore();
  let token = storageHelper.getToken() || null;
  let UserInfo = storageHelper.getUserInfo();
  // let avatar = storageHelper.getUserInfo().avatar;
  // let nickname = storageHelper.getUserInfo().nickname;
  let type_id = $getUrlParams().type_id;
  let url_add = ref("/api/article_type/add");
  let url_set = ref("/api/article_type/set");
  let url_get_obj = ref("/api/article_type/get_obj");
  let url_upload = ref("/api/article_type/upload");
  let title = ref("article_type");
  let field = ref("type_id");
  
  let form = ref({
	type_id: 0,
	icon: "",
	name: "",
	father_id: 0,
	url: "",
	description: ""
  });
  
  let ruleFormRef = ref(null);
  let rules = ref({
	name: [
	  {
		required: true,
		message: "类型名称",
		trigger: "blur",
	  },
	],
  });
  

  // 点击取消
  let cancel = () => {
	router.go(-1);
  };
  // 点击提交
  let Onsubmit = async () => {
	let valid = await ruleFormRef.value.validate();
	if (valid) {
	  // 提交表单
	    if(type_id){
			delete form.value.update_time;
           $post("/api/article_type/set?type_id=" + type_id, form.value).then((res) => {
				if (res.result) {
				ruleFormRef.value.resetFields();
				router.push("/home/article_type/table");
				ElMessage.success("提交成功！");
				} else if (res.error) {
				ElMessage.error(res.error.message);
				}
			});
	 
	    }else{
			$post("/api/article_type/add", form.value).then((res) => {
				if (res.result) {
				ruleFormRef.value.resetFields();
				router.push("/home/article_type/table");
				ElMessage.success("提交成功！");
				} else if (res.error) {
				ElMessage.error(res.error.message);
				}
			});

		}
	} else {
	  ElMessage.error(msg);
	}
  };
  // 获取留言详情
  let get_article_type_obj = async () => {
	var list = await $get("/api/article_type/get_obj?type_id=" + type_id);
	form.value = list.result.obj;
  };
  onMounted(() => {
	if(type_id){
	  get_article_type_obj();
	}
  });
  </script>
  
  <style scoped lang="less">
  .diy_edit {
	width: 100%;
	.edit_article_type_form {
	  max-width: 60%;
	  margin: 90px auto;
	  .fn {
		width: 100%;
	  }
	}
	img {
	  width: 100px;
	  height: 100px;
	}
	.edit_list_article_type_sell {
	  width: 80%;
	  margin: 90px auto;
	}
	.footer {
	  text-align: center;
	}
  }
  </style>
  